home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10106 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  66 lines

  1. Path: news.bridge.net!news
  2. From: psycho@bridge.net (Gary Thompson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: NEwbie: How to return a multi-dimensional array from function?
  5. Date: Fri, 15 Mar 1996 17:16:49 GMT
  6. Organization: BridgenetLC - 305.374.3031 - 100 S. Biscayne Blvd, Miami 
  7. Message-ID: <4ic8pj$c99@news.bridge.net>
  8. References: <4hp273$8bu@news.xs4all.nl> <31404CE9.1A4A@mc.net> <mjs.826298629@hubcap> <4ib78s$6gv@news.bridge.net> <4ic3p3$cl5@news.xs4all.nl>
  9. NNTP-Posting-Host: ppp-ftl1-19.bridge.net
  10. X-Newsreader: Forte Free Agent 1.0.81
  11.  
  12. falstaff@xs4all.nl (Falstaff) wrote:
  13.  
  14. >psycho@bridge.net (Gary Thompson) writes:
  15.  
  16. >>>Two things: (1) If you are going to use the value of tmp outside of Foo(),
  17. >>>then you need to declare
  18. >>>    static tmp[10][5];
  19. >>>Otherwise the storage may disappear when you return from Foo().
  20.  
  21. >>No, declaring it as static will only retain the value in the foo() function, if
  22. >>you call it more than once.  You CANNOT use a value declared in a subfunction in
  23. >>MAIN.  You would have to remove both TMP declarations from MAIN and FOO and
  24. >>declare the thing globally outside of main.
  25.  
  26. >WRONG!!!
  27.  
  28. >If you declare a function
  29.  
  30. >int *demo(void)
  31. >{  static int n=0;
  32. >   n++;
  33. >   return &n;
  34. >}
  35.  
  36. >the caller can use the static variable until the next time your function
  37. >is called.  This is how the standard library function tmpnam() returns,
  38. >for example.
  39.  
  40. Well, of course it will work in your example... you returned the value.  Try
  41. this...
  42.  
  43. main()
  44. {
  45.     foo();
  46.     n++;
  47. }
  48.  
  49. foo()
  50. {
  51.     static int n=0;
  52.     n=1;
  53. }
  54.  
  55. This will give you a compile error.  You cannot use N outside of foo() which was
  56. the point I was making.  However, if you called foo again, N will be 1 before it
  57. get's re-assigned to 1.  
  58.  
  59.                               Gary Thompson
  60.                                "The Psycho"
  61.                              psycho@bridge.net
  62.                         72607.1365@compuserve.com
  63.                        http://www.bridge.net/~psycho
  64.            HTTP://ourworld.compuserve.com/homepages/psychotps
  65.  
  66.